package study.top.common;

import java.io.IOException;
import org.apache.log4j.Logger;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class TopCommon {
	
	static Logger logger = Logger.getLogger(TopCommon.class);	
	
	/**
	 * 占싯삼옙占쏙옙 占쏙옙품占쏙옙 占쌉력받억옙 docid占쏙옙占쏙옙 list占쏙옙 占쏙옙占쏙옙 
	 * -> docid占쏙옙 占싯삼옙占쌔쇽옙 占쏙옙占�占쏙옙품 占쏙옙占쏙옙求占�占쌨소듸옙 占쏙옙占쏙옙占쏙옙 占쏙옙. 
	 * @param product		占쌉뤄옙占싹곤옙 占쏙옙占쏙옙 占싱몌옙占쏙옙 占쏙옙占쌉듸옙 占쏙옙품 占싯삼옙
	 * @param itemNum		占쏙옙 占쏙옙占쏙옙占쏙옙占쏙옙 占쏙옙占쏙옙構占�占쏙옙占쏙옙 占쏙옙품占쏙옙 占쏙옙
	 * @return	String[]	占쏙옙품占쌘듸옙占쏙옙 docid占쏙옙 占쏙옙占쏙옙獵占�占썼열
	 */
	public static String[] getDocid(String product, int itemNum) {
		logger.debug("==========method start >> getDocid");
		
		logger.debug("[PARAMS] product=" + product);
		
		//String url = "http://apis.daum.net/shopping/search?q="+ product +"&sort=pop&result=" + itemNum + "&apikey=B367367097&apikey=e72c6945abe2b00054a617999f2045524d8dd8f1";
		String url = "http://apis.daum.net/shopping/search?q="+ product +"&sort=pop&result=" + itemNum + "&apikey=e72c6945abe2b00054a617999f2045524d8dd8f1";
		String[] list = new String[itemNum];
		
		XmlPullParser parser =  null;
		try {
			
			parser = XmlParser.getParser(url, "UTF-8");
				
			// 占쏙옙占쏙옙 占쏙옙占쏙옙占쏙옙 占쏙옙占�占쏙옙占쏙옙 占쏙옙占쏙옙
			String docid = null;
			int i = 0;
		
			// 占쏙옙占쏙옙 占승깍옙占쏙옙 占싱몌옙
			String tag = null;
		
			// 占싻곤옙 占쌍댐옙 占승깍옙占쏙옙 占쏙옙占쏙옙(占쏙옙占쏙옙占쏙옙 占쏙옙占쏙옙, 占쏙옙占쏙옙占쏙옙 占쏙옙, 占쏙옙占쏙옙占승깍옙, 占쏙옙占승깍옙)
			int type = parser.getEventType();
					
			// 占식쏙옙 처占쏙옙占쏙옙 占쏙옙占쏙옙 占쌥븝옙 占쏙옙占쏙옙
			while(type != XmlPullParser.END_DOCUMENT){
				// 占쏙옙占쏙옙 占승깍옙占쏙옙 占싱몌옙
				tag = parser.getName();
						
				// 占쏙옙占쏙옙 占싻곤옙 占쌍댐옙 占승깍옙占쏙옙 占쏙옙占쏙옙占�占싻깍옙
				switch(type) {
					case XmlPullParser.START_TAG:
							/** 占쏙옙占싹댐옙 占쏙옙占쏙옙占쏙옙 占쏙옙占쏙옙占승그몌옙 占쏙옙占쏙옙占쏙옙 占쏙옙, 占쏙옙占쏙옙 占쏙옙占쏙옙 */
						if(tag.equals("docid")){
							parser.next();
							docid = parser.getText();
						}
						break;
					case XmlPullParser.END_TAG:
						/**
						 *  占쏙옙占쏙옙占쏙옙占쏙옙 占쏙옙占쏙옙占쏙옙 표占쏙옙占싹댐옙 占승그곤옙 占쏙옙占쏙옙퓸占쏙옙占�占쏙옙,
						 *  占쏙옙占쏙옙占�占쏙옙占쏙옙占싶몌옙 占싹놂옙占쏙옙 占쏙옙티占�占쏙옙체占쏙옙 占쏙옙환
						 */					
						if(tag.equals("docid")){	
							list[i++] = docid;
						}
						break;
				}
				// 占쏙옙占쏙옙 占승그뤄옙 占싱듸옙占싹곤옙, 占쏙옙占승곤옙占쏙옙 占쏙옙占쏙옙
				type = parser.next();
			}
					
		}catch(XmlPullParserException e){
			System.out.println("占식쇽옙 占쏙옙 占쏙옙占쏙옙 >> " + e.toString());
			e.printStackTrace();
		}catch (IOException e) {
			System.out.println("占식쇽옙 占싻깍옙 占쏙옙占쏙옙 >> " + e.toString());
			e.printStackTrace();
		}
		
		logger.debug("==========method end >> getDocid");
		return list;
	}
}
